<template>
	<div class="app-container">
		<el-table :data="teacherData" border style="width: 100%;">
		    <el-table-column prop="unid" label="课程编号" width="120"></el-table-column>
			<el-table-column prop="textbook_name" label="教材名称" width="120"></el-table-column>
			<el-table-column prop="textbook_house" label="教材出版社" width="120"></el-table-column>
			<el-table-column prop="public_date" label="出版时间" width="160"></el-table-column>
			<el-table-column prop="section[0].num" label="章节" width="80"></el-table-column>
			<el-table-column prop="know[0].num" label="知识点" width="80"></el-table-column>
			<el-table-column prop="inventory.inventory" label="库存" width="80"></el-table-column>
			<el-table-column prop="institution_id" label="创建人" width="120"></el-table-column>
			<el-table-column prop="create_time" label="创建时间" width="200"></el-table-column>
			<el-table-column prop="update_time" label="最后修改时间" width="200"></el-table-column>
		    <el-table-column fixed="right" label="操作" :width="show?300:210">
		      <template slot-scope="scope">
		        <el-button @click="query(scope.row)" type="primary" size="small">查看</el-button>
				<el-button @click="edit(scope.row)" type="success" size="small">编辑</el-button>
				<el-button @click="ware(scope.row)" type="info" size="small" v-if="show">入库</el-button>
				<el-button @click="show?recycle(scope.row):del(scope.row)" type="danger" size="small">{{show?'回收站':'删除'}}</el-button>
		      </template>
		    </el-table-column>
		</el-table>
		<el-dialog title="入库" :visible.sync="showWare">
			<add-ware :teaData="teaData"></add-ware>
		</el-dialog>
		<el-dialog title="查看" :visible.sync="showQuery">
			<look :teaData="teaData" :tree="tree"></look>
		</el-dialog>
		<el-dialog title="编辑" :visible.sync="showEdit">
			<add-tea :teaData="teaData" ref="edit"></add-tea>
		</el-dialog>
	</div>
</template>

<script>
import { delTeach,queryTeachById } from '@/api/studentRecruit.js'
import addWare from '../ware/addWare.vue'
import look from '../select/look.vue'
import addTea from '../add/addTea.vue'
export default {
	components:{
		addWare,
		look,
		addTea
	},
	data() {
		return{
			teaData:'', //点击按钮的表格信息
			showQuery:false, //查看弹窗
			showWare:false, //入库弹窗
			showEdit:false, //编辑弹窗
			tree:[]
		}
	},
	created() {
		
	},
	methods:{
		// 查看
		async query(row){
			let resData = {
				unid:row.unid
			}
			let data = await queryTeachById(resData)
			if(data.code == 200){
				console.log(data.data);
				this.teaData = data.data
			}
			this.showQuery = true
			let tr1 = []
			let tr2 = []
			let tr3 = []
			for (var i = 0; i < this.teaData.section.length; i++) {
				let treeData = {}
				switch (this.teaData.section[i].level){
					case 1:
					treeData.unid = this.teaData.section[i].unid
					treeData.pid = this.teaData.section[i].pid
					treeData.label = '主章节：'+this.teaData.section[i].section_name
					treeData.children = []
					tr1.push(treeData)
						break;
					case 2:
					treeData.unid = this.teaData.section[i].unid
					treeData.pid = this.teaData.section[i].pid
					treeData.label = '子章节：'+this.teaData.section[i].section_name
					treeData.children = []
					tr2.push(treeData)
						break;
					case 3:
					treeData.unid = this.teaData.section[i].unid
					treeData.pid = this.teaData.section[i].pid
					treeData.label = '栏目：'+this.teaData.section[i].section_name
					treeData.children = []
					tr3.push(treeData)
						break;
					default:
						break;
				}
			}
			//判断子章节是否是该主章节的
			for (var i = 0; i < tr1.length; i++) {
				for (var j = 0; j < tr2.length; j++) {
					if(tr1[i].unid == tr2[j].pid){
						let data = {
							unid:tr2[j].unid,
							pid:tr2[j].pid,
							label:tr2[j].label,
							children:[],
						}
						tr1[i].children.push(data)
					}
				}
			}
			//判断栏目是否是该子章节的
			for (var i = 0; i < tr1.length; i++) {
				for (var j = 0; j < tr1[i].children.length; j++) {
					for (var k = 0; k < tr3.length; k++) {
						if(tr2[j].unid == tr3[k].pid){
							let data = {
								unid:tr3[k].unid,
								pid:tr3[k].pid,
								label:tr3[k].label,
								children:[],
							}
							tr1[i].children[j].children.push(data)
							let data2 = [
								{
									label:'知识点:',
									children:[],
								},{
									label:'考点:',
									children:[],
								}
							]
							for (var g = 0; g < data2.length; g++) {
								tr1[i].children[j].children[k].children.push(data2[g])
							}
							//判断知识点/考点是否是该栏目的
							for (var l = 0; l < this.teaData.know.length; l++) {
								if(tr1[i].children[j].children[k].unid == this.teaData.know[l].section_id){
									console.log(this.teaData.know[l]);
									let data ={}
									switch (this.teaData.know[l].type){
										case 1:
										data.label = this.teaData.know[l].point_name
										tr1[i].children[j].children[k].children[0].children.push(data)
											break;
										case 2:
										data.label = this.teaData.know[l].point_name
										tr1[i].children[j].children[k].children[1].children.push(data)
											break;
										default:
											break;
									}
								}
							}
						}
					}
				}
			}
			/* for (var i = 0; i < this.teaData.know.length; i++) {
				console.log(this.teaData.know[i]);
			} */
			console.log('treeList',tr1);
			this.tree = tr1
		},
		// 编辑
		async edit(row){
			this.showEdit = true
			let resData = {
				unid:row.unid
			}
			let data = await queryTeachById(resData)
			if(data.code == 200){
				console.log('编辑',data.data);
				this.teaData = data.data
			}
			this.$nextTick(() => {
				this.$refs.edit.getMes()
			})
		},
		// 入库
		ware(row){
			this.showWare = true
			this.teaData = row
			console.log('入库',row);
		},
		// 回收站
		async recycle(row){
			this.teaData = row
			console.log('回收站',row);
			let resData = {
				unid:row.unid
			}
			let data = await delTeach(resData)
			if(data.code == 200){
				this.$message.success('删除成功!')
				this.queryTea()
			}else{
				this.$message.error('删除失败!')
			}
		},
		//删除
		async del(row){
			let resData = {
				unid:row.unid
			}
			let data = await delTeach(resData)
			if(data.code == 200){
				this.$message.success('删除成功!')
				this.queryTea()
			}else{
				this.$message.error('删除失败!')
			}
		},
	},
	props:{
		teacherData:{
			type:Array,
			default:[],
		},
		show:{
			type:Boolean,
			default:false,
		},
		queryTea:{
			type:Function,
			default:() => {}
		}
	}
}
</script>

<style>
	
</style>
